
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace  Syrinx.Gui
{
	[ToolboxData("<{0}:BizObjListBoxDisplay runat=server></{0}:BizObjListBoxDisplay>")]
    public class BizObjListBoxDisplay : WebControl, Swaf.Gui.IBizObjListDisplay
	{
        protected ListBox m_listBox;
		protected IBizObjList m_list;
        protected IBizObj m_selectedBizObj;
		protected string m_fldName;
		protected int m_maxElements = -1;
        protected bool m_fireEvent = false;

		public BizObjListBoxDisplay()
		{
            this.Load += new EventHandler(BizObjListBoxDisplay_Load);
            this.PreRender+=new EventHandler(BizObjListBoxDisplay_PreRender);
            m_listBox = new ListBox();
        }

		protected string m_parentIds;
		protected BizProcWidgetType m_procWidgetType = BizProcWidgetType.list;
		public string ParentBizProcIds { get { return m_parentIds; } set { m_parentIds = value; } }
		public BizProcWidgetType BizProcWidgetType { get { return m_procWidgetType; } set { m_procWidgetType = value; } }
		
		void BizObjListBoxDisplay_Load(object sender, EventArgs e)
        {
            m_listBox.ID = UniqueID;
            m_listBox.EnableViewState = false;
            m_listBox.Attributes.Add("onchange",
                "javascript:setTimeout('__doPostBack(\\'" + this.UniqueID + "\\',\\'\\')',0)");
        }


        void  BizObjListBoxDisplay_PreRender(object sender, EventArgs e)
        {
            if(m_fireEvent && BizObjSelected != null)
                BizObjSelected(this, m_selectedBizObj, "");
        }

		void UserListDisplay_SelectedIndexChanged(object sender, EventArgs e)
		{
			BizObjSelected(this,SelectedBizObj,"");
		}

        protected override void Render(HtmlTextWriter writer)
        {
            m_listBox.RenderControl(writer);
        }
		
		#region IListGuiDisplay Members

        public void showBizObjs(IBizObjCursor list)
		{
			m_list = list.Source;

            string selectedId = Page.Request.Form[UniqueID];
            if (m_selectedBizObj != null)
                selectedId = m_selectedBizObj.KeyValue.ToString();

			if (m_list != null)
			{
				m_listBox.Items.Clear();
                while(!list.eof)
				{
                    IBizObj bizObj = list.currentElement;
                    list.moveNext();

                    ListItem itm = new ListItem();
                    itm.Text = bizObj.get(m_fldName).ToString();
                    itm.Value = bizObj.KeyValue.ToString();
                    if (itm.Value == selectedId)
                    {
                        itm.Selected = true;
                        m_selectedBizObj = bizObj;
                        if (selectedId == null || !selectedId.Equals(ViewState["sid"]))
                        {
                            m_fireEvent = true;
                            ViewState["sid"] = selectedId;
                        }

                    }
                    m_listBox.Items.Add(itm);
				}
			}
		}

		public void addBizObj(IBizObj bizObj)
		{
			if (bizObj != null)
			{
				ListItem itm = new ListItem();
				itm.Value = bizObj.get("id").ToString();
				itm.Text = bizObj.get(m_fldName).ToString();
				m_listBox.Items.Add(itm);
				setSelectedObj(bizObj);
			}
		}

		public void removeBizObj(IBizObj bizObj)
		{
			//todo: write implementation to remove the give bizObj from
			// the list and from m_list cache;
		}

		public IBizObj SelectedBizObj
		{
			get
			{
                if (m_selectedBizObj != null)
                {
                    string id = m_listBox.SelectedValue;
                    if (id != null && id.Length != 0)
                        return m_list.search(m_list.objectType.KeyFieldNames, SelectedBizObjKeyValue);
                    else
                        return null;
                }
                return m_selectedBizObj;
			}
			set{setSelectedObj(value);}
		}

        public object SelectedBizObjKeyValue 
        { 
            get 
            {
                string selectedId = Page.Request.Form[UniqueID];
                if (selectedId == "")
                    selectedId = null;
                if (m_list == null || selectedId == null)
                    return selectedId;
                else
                    return m_list.objectType.convertValueToKeyType(selectedId);
            }
			set { } 
        }

        public void getSelectedBizObjs(IBizObjList selected)
        {
            //TODO: Implement this method
        }
		public void setSelectedBizObjs(IBizObjList selected)
		{
		}

		public virtual void updateBizObjs()
		{
			//Intentionally left blank. This class is read only.
		}


        public event Swaf.Gui.BizObjSelectedInfo BizObjSelected;
		public event Swaf.Gui.BizObjChanged BizObjUpdated;

		public event Swaf.Gui.BizObjChanged BizObjDeleted;

		public event Swaf.Gui.BizObjChanged BizObjAdded;

		protected void setSelectedObj(IBizObj obj)
		{
			if (m_fldName != null && m_fldName.Length > 0)
                for (int i = 0; i < m_listBox.Items.Count; i++)
                    if (m_listBox.Items[i].Value == obj.get("id").ToString())
					{
						m_listBox.SelectedIndex = i;
						m_listBox.SelectedItem.Text = obj.get(m_fldName).ToString();
						break;
					}
		}

        public Privileges EditPrivilege
        {
            get
            {
                return Privileges.ReadOnly;
            }
            set
            {  }
        }

		#endregion

		#region IWidget Members

		public string WidgetId{get { return this.UniqueID; }}
        public int MaxElements { get { return m_maxElements; } set { m_maxElements = value; } }
		#endregion			
	
	}
}
